//
//  EcgListCell.m
//  CareRingApp
//
//  Created by Linktop on 2024/4/11.
//

#import "EcgListCell.h"
#import "TimeUtils.h"
#import "ConfigModel.h"
@implementation EcgListCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        [self baseLayout];
    }
    return self;
}

-(void)baseLayout
{
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    self.contentView.backgroundColor = UIColor.clearColor;
    self.backgroundColor = UIColor.clearColor;

    UIView *bgView = [UIView new];
    bgView.backgroundColor = ITEM_BG_COLOR;
    [self.contentView addSubview:bgView];
    
    
    
    [self.contentView addSubview:self.topConcluLbl];
    [self.contentView addSubview:self.timeBtn];
    [self.contentView addSubview:self.hrItem];
    [self.contentView addSubview:self.symptonCountLbl];
    
    [self.contentView addSubview:self.hmEcgDrawLineView];
    [self.contentView addSubview:self.gridView];


    [bgView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.bottom.equalTo(bgView.superview);
        make.top.equalTo(bgView.superview.mas_top).offset(15);
        
    }];
    
    [self.topConcluLbl mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(bgView.mas_top).offset(2);
        make.leading.equalTo(self.gridView.mas_leading);
        make.height.equalTo(@35);
    }];
    
    [self.timeBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.topConcluLbl.mas_top);
        make.trailing.equalTo(self.gridView.mas_trailing);
    }];
    
    [self.hrItem mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.topConcluLbl);
        make.top.equalTo(self.topConcluLbl.mas_bottom).offset(3);
        make.height.lessThanOrEqualTo(@20);
    }];
    
    [self.symptonCountLbl mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.trailing.equalTo(self.gridView.mas_trailing);
        make.centerY.equalTo(self.hrItem.mas_centerY);
    }];
    
    [self.hmEcgDrawLineView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.top.bottom.equalTo(self.gridView);
    }];
    
    CGFloat height = [self.gridView lengthBySquareCount:5 * 4];// 高度
    //宽度
    CGFloat width = [[self.gridView class] integetLengthFrom:self.contentView.bounds.size.width];
    [self.gridView mas_remakeConstraints:^(MASConstraintMaker *make) {
//        make.leading.equalTo(self.gridView.superview.mas_leading).offset(10);
//        make.trailing.equalTo(self.gridView.superview.mas_trailing).inset(10);
        make.width.equalTo(@(width));
        make.centerX.equalTo(self.gridView.superview.mas_centerX);
        make.top.equalTo(self.hrItem.mas_bottom).offset(10);
//        make.bottom.equalTo(bgView.mas_bottom).inset(8);
        make.height.equalTo(@(height));
    }];
    
}

-(void)setEcgRecord:(DBEcgRecord *)ecgRecord
{
    _ecgRecord = ecgRecord;
    
    // 时间
    [self formatTimeString];
    
    self.topConcluLbl.text = [DBEcgRecord titleOfConclusion:_ecgRecord.conclusion];
    
    if (_ecgRecord.avgHeartRate.intValue) {
        self.hrItem.titleLbl.text = [NSString stringWithFormat:@"%@ %d%@", _L(L_AVG), self.ecgRecord.avgHeartRate.intValue, _L(L_UNIT_HR)];

    } else {
        self.hrItem.titleLbl.text = [NSString stringWithFormat:@"%@ %@%@", _L(L_AVG), NONE_VALUE, _L(L_UNIT_HR)];
    }
    
    [self drawWave];
    
    if (_ecgRecord.symptomsArray.count) {
        
        if (_ecgRecord.symptomsArray.count > 1) {
            // 复数
            self.symptonCountLbl.text = [NSString stringWithFormat:_L(L_ITEM_SYMPTOM_S), (int)_ecgRecord.symptomsArray.count];
            self.symptonCountLbl.textColor = BUTTON_RED;
        } else {
            if (_ecgRecord.symptomsArray.firstObject.intValue == SYMPTOM_NONE) {
                self.symptonCountLbl.text = _L(L_NO_SYMPTOMS);
                self.symptonCountLbl.textColor = UIColor.lightGrayColor;//BUTTON_RED;

            } else {
                self.symptonCountLbl.text = [NSString stringWithFormat:_L(L_ITEM_SYMPTOM), (int)_ecgRecord.symptomsArray.count];
                self.symptonCountLbl.textColor = BUTTON_RED;

            }
            

        }
        
    } else {
        self.symptonCountLbl.text = nil;
    }
   
}

-(void)drawWave {
    [self.contentView layoutIfNeeded];
    [self.hmEcgDrawLineView layoutIfNeeded];
    // 画图 从第 10s - 20s
    int begin = 0;
    int end = (int)self.ecgRecord.volArray.count;
    
    if (self.ecgRecord.volArray.count > 10 * 512) {
        begin = 10 * 512;
    }
    
    if (self.ecgRecord.volArray.count > 20 * 512) {
        end = 20 * 512;
    }
    
    
    NSArray<NSNumber *> *subWaves = [self.ecgRecord.volArray subarrayWithRange:NSMakeRange(begin, end)];
    self.hmEcgDrawLineView.direction = self.ecgRecord.wearHand;

    [self.hmEcgDrawLineView drawAllData:subWaves];
}

-(void)formatTimeString {
    NSDateFormatter *fmt = [[NSDateFormatter alloc]init];
   
    NSDate *recDate = [NSDate dateWithTimeIntervalSince1970:self.ecgRecord.startTs.doubleValue];
    NSDate *date = [NSDate date];
    fmt.dateFormat = @"MM/dd HH:mm";

   
    [self.timeBtn setTitle:[fmt stringFromDate:recDate] forState:UIControlStateNormal];
    
    
    
    
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

-(ImgTitleItem *)hrItem
{
    if (!_hrItem) {
        _hrItem = [[ImgTitleItem alloc]init];
        _hrItem.imageView.image = [UIImage imageNamed:@"ecg_hr"];
        _hrItem.titleLbl.font = [UIFont fontWithName:FONT_ARIAL_BOLD size:17];
        
    }
    return _hrItem;
}

-(QMUIButton *)timeBtn
{
    if (!_timeBtn) {
        _timeBtn = [[QMUIButton alloc]init];
        [_timeBtn setTitleColor:UIColor.lightGrayColor forState:UIControlStateNormal];
        [_timeBtn setImage:[UIImage imageNamed:@"arrow_right"] forState:UIControlStateNormal];
        _timeBtn.imagePosition = QMUIButtonImagePositionRight;
        _timeBtn.spacingBetweenImageAndTitle = 3;
        
    }
    return _timeBtn;
}

-(UILabel *)topConcluLbl
{
    if (!_topConcluLbl) {
        _topConcluLbl = [UILabel new];
        _topConcluLbl.textAlignment = NSTextAlignmentLeft;
        _topConcluLbl.text = @"_topConcluLbl";
    }
    return _topConcluLbl;
}

-(UILabel *)symptonCountLbl
{
    if (!_symptonCountLbl) {
        _symptonCountLbl = [UILabel new];
        _symptonCountLbl.textAlignment = NSTextAlignmentRight;
        _symptonCountLbl.textColor = BUTTON_RED;
        _symptonCountLbl.text = @"2";
    }
    return _symptonCountLbl;
}

-(LTHmEcgGridView *)gridView
{
    if (!_gridView) {
        _gridView = [[LTHmEcgGridView alloc]init];
    }
    return _gridView;
}

-(LTRingEcgDrawLineView *)hmEcgDrawLineView
{
    if (!_hmEcgDrawLineView) {
        LTRingEcgDrawLineView *lineView = [[LTRingEcgDrawLineView alloc]init];
        lineView.backgroundColor = UIColor.clearColor;//ECG_BACKGROUND_COLOR_DEFAULT;
        //suggest to use HORIZONTAL_SPEED_DEFAULT and VERTIAL_GAIN_DEFAULT
        
//        NSNumber *spType = [AccountCenter instance].currentAccount.otherInfo.ecgXspeedType;
//        NSNumber *ygainType = [AccountCenter instance].currentAccount.otherInfo.ecgYgainType;
//
//        
//        [lineView setHorizonalSpeed:spType.unsignedIntValue VertialGain:ygainType.unsignedIntValue];
        
        lineView.ecgLineColor = BUTTON_RED;
        _hmEcgDrawLineView = lineView;
    }
    
    return _hmEcgDrawLineView;
}

@end
